﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

using ExpLib.Web.SSO; 

namespace ExpLib.SSOPortal
{
    public partial class SignIn : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (Request.IsAuthenticated)
            {
                String signData = GetSignDataFormTicket();
                ReturnSourceUrl(signData);
            }
        }

        protected void LoginCtrl_Authenticate(object sender, AuthenticateEventArgs e)
        {
            bool authResult = Authenticate(LoginCtrl.UserName, LoginCtrl.Password);

            if (authResult)
            {
                e.Authenticated = true;

                String signData = GetSignData(LoginCtrl.UserName);
                ReturnSourceUrl(signData);
            }
            else
            {
                e.Authenticated = false;
            }
        }

        private bool Authenticate(String userName, String password)
        {
            return true ;
        }

        private String GetSignData(String userName)
        {
            SetTicket();
            return userName + "$" + "";
        }

        private String GetPrivateKeyXml()
        {
            return "<RSAKeyValue><Modulus>n01P4RTisCpUKg+XQWEuOFSBKNWPxsxEZHVVbXl07p6lfdqBAfmgVtetMtKSCFwhLD8sROosoWDqf7ZrniBg1LAYyFpdik4XEAKXuy+qnfB9EnQCvM3LluJlk/Cz1JgEBTy5XKLszfeG5+Xo4QD9VxikOxiapZSo5qg9HML8OXE=</Modulus><Exponent>AQAB</Exponent><P>y4doVLfUOX+J5EUlmN4R+2IvYIjHaVn3rLYCrEgbQ3GAm9fHRc/BtVl6Oon77R3TSuwcybfUlNRG+QKxyctHaw==</P><Q>yF7+5Tf/Oatu4f8CdxmHLY9obb9F/fN+kNPY7HBRmTwopv3f3VyOdSQewpB5af8a1vIIGFM8NMF6H2hbRn9lkw==</Q><DP>U/MOPaN1jiHlJpq+30b5886T7CV+2x2Y40xP3fAADtmKSr2+HDWNmNfHas3vc+aReJ3bYfjJXnoQrDVIUAvg8Q==</DP><DQ>YYMJnVfRF6m8frNx6bA8vwlpku9hCqOzVpwil35Z168VQZ+PkNT4Zz260oko7VK/JxgGpH/tbucxnZNVyEs7jQ==</DQ><InverseQ>f3F2gytVjOLm9EKkFFPktR8kSQ6g+0KQ07h23hrEKUk03puNsXAVR7knmKxv7rrl26Ry4MYlLPiJCVbJX4Bx1g==</InverseQ><D>cDYxUkc39weaCZa54N/ngt5jrLPidxJkCq9r4ZVwfLVbtW84+H8yJnn+EpKpMyJSa6yELncVwQdXp2aEH/qGua5e/nOtVOSY8TUZWcU2gONeqKIdAnib17rI2u1+te8MQfWgJLU8jmDjcaYj4UrkPIu3tOXx9cmmTwsj2NLyphE=</D></RSAKeyValue>";
        }

        private void SetTicket()
        { 
        
        }

        private String GetSignDataFormTicket()
        {
            return String.Empty;
        }

        private void ReturnSourceUrl(String signData)
        {
            using (SignHelper signHelper = new SignHelper())
            {
                signHelper.FromXmlString(GetPrivateKeyXml());

                String salt = Request.Params["salt"];
                String returnUrl = Request.QueryString["ReturnUrl"];
                String signedData = signHelper.SignatueData(signData + salt);

                if (String.IsNullOrEmpty(returnUrl))
                {
                    if (returnUrl.IndexOf("?") > 0)
                    {
                        returnUrl = returnUrl + "&s=" + signedData + "d=" + signData;
                    }
                    else
                    {
                        returnUrl = returnUrl + "?s=" + signedData + "d=" + signData;
                    }

                    Response.Redirect(returnUrl, false);
                }

            }
        }


    }
}